package com.biostime.terminal.util;

//import com.mama100.merchandise.thrift.service.Query;
//import com.mama100.merchandise.vo.domain.ChargeCate;
//import com.mama100.merchandise.vo.domain.ChargePayment;
//import com.mama100.merchandise.vo.inout.I64ChargeCateListResponse;
//import com.mama100.merchandise.vo.inout.I64ChargePaymentListResponse;

import org.dozer.DozerBeanMapper;

import java.util.*;

/**
 * <p>
 * Dozer Mapper 封装
 * </p>
 *
 * @author yangrzh 2014/6/5
 */
public class Mapper {

    private org.dozer.DozerBeanMapper mapper;

    private Mapper() {
        mapper = new DozerBeanMapper();
        List<String> mappers = new ArrayList<String>();  
        mappers.add("VoMapping.xml"); 
        mapper.setMappingFiles(mappers);
    }

    private static class SingletonHolder
    {
        public final static Mapper instance = new Mapper();
    }

    public static Mapper getInstance()
    {
        return SingletonHolder.instance;
    }

    public <T> T map(Object source, Class<T> destinationClass) {
        if (source == null) {
            return null;
        }
        return mapper.map(source, destinationClass);
    }

    /*public <T> List<T> map(List source, Class<T> clz) {
        if (source == null) {
            return null;
        }
        if (clz.equals(com.mama100.rpc.merchandise.thrift.inout.Query.class)) {
            QueryUtil.QueryCounter id = new QueryUtil.QueryCounter(0);
            List<com.mama100.merchandise.vo.service.Query> handledQuerys = QueryUtil.handleQuery(source, id);
            List target = new ArrayList();
            for (Object o : handledQuerys) {
                com.mama100.rpc.merchandise.thrift.inout.Query to = map(o, com.mama100.rpc.merchandise.thrift.inout.Query.class);
                target.add(to);
            }
            return target;
        } else {
            List target = new ArrayList();
            for (Object o : source) {
                Object to = map(o, clz);
                target.add(to);
            }
            return target;
        }
    }*/

    public <T, V> Map<T, V> map(Map map, Class<T> keyClz, Class<V> valueClz) {
        if (map == null) {
            return null;
        }
        Map<T, V> target = new HashMap<T, V>();
        Set<Map.Entry> entrySet = map.entrySet();
        for (Map.Entry entry : entrySet) {
            T key = map(entry.getKey(), keyClz);
            V value = map(entry.getValue(), valueClz);
            target.put(key, value);
        }
        return target;
    }

//    public I64ChargePaymentListResponse map(com.mama100.merchandise.thrift.inout.I64ChargePaymentListResponse response) {
//        I64ChargePaymentListResponse targetResponse = new I64ChargePaymentListResponse();
//        Map<Long, List<com.mama100.merchandise.thrift.domain.ChargePayment>> map = response.getResponse();
//        Map<Long, List<ChargePayment>> targetMap = new HashMap<Long, List<ChargePayment>>();
//        Set<Map.Entry<Long, List<com.mama100.merchandise.thrift.domain.ChargePayment>>> entrySet = map.entrySet();
//        Iterator<Map.Entry<Long, List<com.mama100.merchandise.thrift.domain.ChargePayment>>> it = entrySet.iterator();
//        while (it.hasNext()) {
//            Map.Entry<Long, List<com.mama100.merchandise.thrift.domain.ChargePayment>> entry = it.next();
//            Long key = entry.getKey();
//            List<com.mama100.merchandise.thrift.domain.ChargePayment> list = entry.getValue();
//            targetMap.put(key, map(entry.getValue(), ChargePayment.class));
//        }
//        targetResponse.setResultCode(response.getResultCode());
//        targetResponse.setResponse(targetMap);
//        return targetResponse;
//    }
//
//    public I64ChargeCateListResponse map(com.mama100.merchandise.thrift.inout.I64ChargeCateListResponse response) {
//        I64ChargeCateListResponse targetResponse = new I64ChargeCateListResponse();
//        Map<Long, List<com.mama100.merchandise.thrift.domain.ChargeCate>> map = response.getResponse();
//        Map<Long, List<ChargeCate>> targetMap = new HashMap<Long, List<ChargeCate>>();
//        Set<Map.Entry<Long, List<com.mama100.merchandise.thrift.domain.ChargeCate>>> entrySet = map.entrySet();
//        Iterator<Map.Entry<Long, List<com.mama100.merchandise.thrift.domain.ChargeCate>>> it = entrySet.iterator();
//        while (it.hasNext()) {
//            Map.Entry<Long, List<com.mama100.merchandise.thrift.domain.ChargeCate>> entry = it.next();
//            Long key = entry.getKey();
//            List<com.mama100.merchandise.thrift.domain.ChargeCate> list = entry.getValue();
//            targetMap.put(key, map(entry.getValue(), ChargeCate.class));
//        }
//        targetResponse.setResultCode(response.getResultCode());
//        targetResponse.setResponse(targetMap);
//        return targetResponse;
//    }

}
